package com.appjishu.web.controller;

import com.appjishu.passport.intercepter.LoginInterceptor;
import com.appjishu.web.biz.dao.UserInfoMapper;
import com.appjishu.web.biz.service.IUserInfoService;
import com.appjishu.web.model.Demo;
import com.appjishu.web.model.PageBean;
import com.appjishu.web.model.User;
import com.appjishu.web.model.UserInformation;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户信息Controller
 *
 * @author neuedu
 * @date 2023-12-13
 */
@Api(tags = {"用户信息"})
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @Resource
    private IUserInfoService userInfoService;

    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 查询用户信息
     */
    @GetMapping("/list")
    @ApiOperation("查询用户信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "total", value = "总记录数"),
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "rows", value = "返回业务数据（数组类型）", dataTypeClass = User.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    public PageBean list(User user, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        //分页

/*        PageHelper.startPage(pageNum,pageSize);
        System.out.println(pageSize);
        List<User> users = userInfoService.selectUserInfolist(user);
        List<User> selects = userInfoMapper.selects();
        PageInfo pageInfo = new PageInfo(selects);
        System.out.println("总页数: " + pageInfo.getPages());
        System.out.println("总记录数: " + pageInfo.getTotal());
        System.out.println("当前页数: " + pageInfo.getPageNum());
        System.out.println("当前页面记录数量: " + pageInfo.getSize());*/
/*        Page<User> p = (Page<User>) users;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());*/

        Long count = userInfoMapper.count();
        int start = (pageNum - 1) * pageSize;
        List<User> pagelist = userInfoMapper.page(start, pageSize);
        PageBean pageBean = new PageBean(count, pagelist);
        return pageBean;
        /*return pageInfo.getList();*/
    }

    /**
     * 删除用户信息
     */
    @ApiOperation("删除用户信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @DeleteMapping("/{ids}")
    public int remove(@PathVariable Long[] ids) {
        int i = userInfoService.deleteUserInfoByIds(ids);
        return i;
    }


    /**
     * 修改用户信息
     */
    @ApiOperation("修改用户信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Boolean edit(@RequestBody User user) {
        int i = userInfoMapper.updateNameAndClassNameAndUsernameAndEmailAndPersonalSignature(user.getName(), user.getClassName(), user.getUsername(), user.getEmail(), user.getPersonalSignature(), user.getId());
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 获取用户信息详细信息
     */
    @ApiOperation("获取用户信息详细信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "data", value = "返回业务数据", dataTypeClass = User.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @GetMapping(value = "/{id}")
    public User getInfo(@PathVariable("id") Long id) {
        User user = userInfoMapper.selectAllById(id);
        return user;
    }

    /**
     * 新增用户信息
     */
    @ApiOperation("新增用户信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PostMapping("/add")
    public Boolean add(@RequestBody User user) {
        int i = userInfoMapper.insertSelective(user);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 批量新增用户信息
     */
    @ApiOperation("批量新增用户信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PostMapping("/adds")
    public Boolean adds(@RequestBody List<User> user) {
        int i = userInfoMapper.batchInsert(user);
        System.out.println(i);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 用户信息积分前五
     */
    @GetMapping("/topfive")
    @ApiOperation("用户信息积分前五")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "total", value = "总记录数"),
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "rows", value = "返回业务数据（数组类型）", dataTypeClass = User.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    public List<Demo> topfive(Demo demo) {
        List<Demo> select = userInfoMapper.select(demo);
        return select;
    }

    @PostMapping("/getuserlog")
    @ApiOperation("获取用户登录记录")
    public List<UserInformation> getUserLog() {
        Long userId = LoginInterceptor.getUserId();
        List<UserInformation> userInformationsList = userInfoService.getUserLog(userId);
        return userInformationsList;
    }
}
